﻿using System.IO;
using System.Text;

namespace WpfMvvmTemplate.WPFUI.Tools.Helper
{
    public class LogHelper
    {
        public static void WriteLog(string content, string? fileName = null)
        {
            fileName ??= DateTime.Now.ToString("yyyy_MM_dd");

            string text = Directory.GetCurrentDirectory() + "\\Logs";
            if (!Directory.Exists(text))
            {
                Directory.CreateDirectory(text);
            }

            for (int i = 0; i < 5; i++)
            {
                try
                {
                    File.AppendAllText(text + "\\" + fileName + ".txt", "----------------------------------------------------------------------------------------------------------\r\n时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + content + "\r\n----------------------------------------------------------------------------------------------------------\r\n", Encoding.UTF8);
                    return;
                }
                catch (Exception)
                {
                    Thread.Sleep(20);
                }
            }
        }

    }
}
